package com.hkbigdata.flinkcdc;

import com.alibaba.ververica.cdc.connectors.mysql.MySQLSource;
import com.alibaba.ververica.cdc.connectors.mysql.table.StartupOptions;
import com.alibaba.ververica.cdc.debezium.DebeziumSourceFunction;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;

/**
 * @author liuanbo
 * @creat 2024-05-23-14:52
 * @see 2194550857@qq.com
 */
public class Flink01_CDC {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment().setParallelism(1);

        System.setProperty("HADOOP_USER_NAME", "hkbigdata");

        DebeziumSourceFunction<String> build = MySQLSource.<String>builder()
                .hostname("hadoop102")
                .port(3306)
                .username("root")
                .password("123456")
                .databaseList("test", "user_profile_manager_test")
                .tableList("test.order_info", "test.sensor")
                .startupOptions(StartupOptions.initial())
                //反序列化的功能：解析binglog 里面日志数据，得到咱们想要输出到FlinkCDC的格式
                .deserializer(new MyDeserializerFunc())
                .build();

        DataStreamSource<String> stringDataStreamSource = env.addSource(build);

        stringDataStreamSource.print();


        stringDataStreamSource.addSink(new FlinkKafkaProducer<String>("hadoop102:9092", "flink215", new SimpleStringSchema()));

        env.execute();


    }
}
